package com.glorysoft.knowlespda.ui.eqpauthor.view

import android.os.Bundle
import android.widget.TextView
import androidx.databinding.ViewDataBinding
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.glorysoft.knowlespda.BR
import com.glorysoft.knowlespda.R
import com.glorysoft.knowlespda.base.BaseActivity
import com.glorysoft.knowlespda.base.Constant
import com.glorysoft.knowlespda.databinding.ActivityEqpAuthorizationBinding
import com.glorysoft.knowlespda.ui.eqpauthor.viewmodel.EqpAuthorizationViewModel
import com.glorysoft.knowlespda.utils.FreeApi_DialogUtils
import kotlinx.android.synthetic.main.activity_eqp_authorization.*

/**
 * FileName: EqpAuthorizationActivity
 * Author: XJH
 * Date: 2022/7/13 10:07
 * Description:
 * History:
 */
class EqpAuthorizationActivity : BaseActivity<EqpAuthorizationViewModel,ActivityEqpAuthorizationBinding>() {
    override fun layoutId(): Int {
        return R.layout.activity_eqp_authorization
    }

    override fun initVariableId(): Int {
        return BR.eqpAuthorizationViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(this.getString(R.string.cewa))
        tvPdaNo.text = Constant.PDA_NO

        tvUpdate.setOnClickListener{
            showDialogs().show()
        }
    }

    override fun initData() {
        viewModel.getPdaAuthInfo()

        viewModel.defUI.startActivity.observe(this){
            val tipDialog = FreeApi_DialogUtils.getConfirmDialog(this, this.getString(R.string.confirm), this.getString(R.string.operation_succeeded),this.getString(R.string.tips)) {
                finish()
            }
            tipDialog.show(this.supportFragmentManager, "DIALOG")
        }
    }

    private fun showDialogs() : MaterialDialog {
        val dialog =  MaterialDialog(this)
            .cancelable(false)
            .customView(R.layout.dialog_confirm_cancel, noVerticalPadding = true)
            .lifecycleOwner(this)
        val contentView = dialog.getCustomView()
        val tvConfirm: TextView = contentView.findViewById(R.id.tvConfirm)
        val tvCancel: TextView = contentView.findViewById(R.id.tvCancel)
        val tvTitle: TextView = contentView.findViewById(R.id.tvTitle)
        val tvContent: TextView = contentView.findViewById(R.id.tvContent)

        tvTitle.text = this.getString(R.string.machine_authorization)
        tvContent.text = this.getString(R.string.confirm_update)
        tvConfirm.text = this.getString(R.string.confirm)
        tvCancel.text = this.getString(R.string.cancel)

        tvCancel.setOnClickListener {
            dialog.dismiss()
        }


        tvConfirm.setOnClickListener {
            dialog.dismiss()
            viewModel.updateAuth()
        }

        return dialog
    }
}